<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="线路名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="线路名称"></el-input>
    </el-form-item>
    <el-form-item label="线路编号" prop="lineNumber">
      <el-input v-model="dataForm.lineNumber" placeholder="线路编号"></el-input>
    </el-form-item>
    <el-form-item label="所属机构" prop="agencyId">
      <el-input v-model="dataForm.agencyId" placeholder="所属机构"></el-input>
    </el-form-item>
    <el-form-item label="线路类型" prop="transportLineTypeId">
      <el-input v-model="dataForm.transportLineTypeId" placeholder="线路类型"></el-input>
    </el-form-item>
    <el-form-item label="起始地机构id" prop="startAgencyId">
      <el-input v-model="dataForm.startAgencyId" placeholder="起始地机构id"></el-input>
    </el-form-item>
    <el-form-item label="目的地机构id" prop="endAgencyId">
      <el-input v-model="dataForm.endAgencyId" placeholder="目的地机构id"></el-input>
    </el-form-item>
    <el-form-item label="距离" prop="distance">
      <el-input v-model="dataForm.distance" placeholder="距离"></el-input>
    </el-form-item>
    <el-form-item label="成本" prop="cost">
      <el-input v-model="dataForm.cost" placeholder="成本"></el-input>
    </el-form-item>
    <el-form-item label="预计时间（分钟）" prop="estimatedTime">
      <el-input v-model="dataForm.estimatedTime" placeholder="预计时间（分钟）"></el-input>
    </el-form-item>
    <el-form-item label="状态 0：禁用 1：正常" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态 0：禁用 1：正常"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          lineNumber: '',
          agencyId: '',
          transportLineTypeId: '',
          startAgencyId: '',
          endAgencyId: '',
          distance: '',
          cost: '',
          estimatedTime: '',
          status: ''
        },
        dataRule: {
          name: [
            { required: true, message: '线路名称不能为空', trigger: 'blur' }
          ],
          lineNumber: [
            { required: true, message: '线路编号不能为空', trigger: 'blur' }
          ],
          agencyId: [
            { required: true, message: '所属机构不能为空', trigger: 'blur' }
          ],
          transportLineTypeId: [
            { required: true, message: '线路类型不能为空', trigger: 'blur' }
          ],
          startAgencyId: [
            { required: true, message: '起始地机构id不能为空', trigger: 'blur' }
          ],
          endAgencyId: [
            { required: true, message: '目的地机构id不能为空', trigger: 'blur' }
          ],
          distance: [
            { required: true, message: '距离不能为空', trigger: 'blur' }
          ],
          cost: [
            { required: true, message: '成本不能为空', trigger: 'blur' }
          ],
          estimatedTime: [
            { required: true, message: '预计时间（分钟）不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态 0：禁用 1：正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/base/transportline/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.transportLine.name
                this.dataForm.lineNumber = data.transportLine.lineNumber
                this.dataForm.agencyId = data.transportLine.agencyId
                this.dataForm.transportLineTypeId = data.transportLine.transportLineTypeId
                this.dataForm.startAgencyId = data.transportLine.startAgencyId
                this.dataForm.endAgencyId = data.transportLine.endAgencyId
                this.dataForm.distance = data.transportLine.distance
                this.dataForm.cost = data.transportLine.cost
                this.dataForm.estimatedTime = data.transportLine.estimatedTime
                this.dataForm.status = data.transportLine.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/base/transportline/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'lineNumber': this.dataForm.lineNumber,
                'agencyId': this.dataForm.agencyId,
                'transportLineTypeId': this.dataForm.transportLineTypeId,
                'startAgencyId': this.dataForm.startAgencyId,
                'endAgencyId': this.dataForm.endAgencyId,
                'distance': this.dataForm.distance,
                'cost': this.dataForm.cost,
                'estimatedTime': this.dataForm.estimatedTime,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
